//Documentation complete
/**
 * @author Jeff
 * @version 2009AP05
 */

package control;

import broker.*;
import problemDomain.*;
import static problemDomain.ErrorBean.*;
import static problemDomain.ErrorBean.ErrorCode.*;

import java.util.*;

/**
 * Control object for payment object operations.
 */
public class PaymentControl extends Control {
	
	/* Attributes */
	
	private PaymentBroker pb;
	
	/* END Attributes */
	
	
	/* Constructors */
	
	/**
	 * Constructs a <code>PaymentControl</code>.
	 * @throws ErrorBean if the database is not available
	 */
	public PaymentControl () throws ErrorBean {
		pb = PaymentBroker.getBroker();
	}
	
	/* END Constructors */
	
	
	/* Methods */
	
	/**
	 * Creates a new payment.
	 * @param orderId the ID of the order with which this payment is associated
	 * @param paymentMethod the method of the payment
	 * @param paymentStatus the status of the payment
	 * @param datePaid the date the payment was made
	 * @param amount the amount of the payment
	 * @throws ErrorBean if the input data are not correct
	 */
	public void create (String orderId, String paymentMethod, String paymentStatus, String datePaid, String amount) throws ErrorBean {
		PaymentBean payment = new PaymentBean(
			0,
			new OrderBean(checkOrderID(orderId)),
			paymentMethod,
			paymentStatus,
			checkDatePaid(datePaid),
			checkAmount(amount));
		pb.persist(payment);
	}
	
	/**
	 * Updates a payment.
	 * @param paymentId thi ID of the payment
	 * @param orderId the ID of the order with which this payment is associated
	 * @param paymentMethod the method of the payment
	 * @param paymentStatus the status of the payment
	 * @param datePaid the date the payment was made
	 * @param amount the amount of the payment
	 * @throws ErrorBean if the input data are not correct
	 */
	public void update (String paymentId, String orderId, String paymentMethod, String paymentStatus, String datePaid, String amount) throws ErrorBean {
		PaymentBean payment = new PaymentBean(
			checkPaymentID(paymentId),
			new OrderBean(checkOrderID(orderId)),
			paymentMethod,
			paymentStatus,
			checkDatePaid(datePaid),
			checkAmount(amount));
		pb.persist(payment);
	}
	
	/**
	 * Deletes a payment.
	 * @param paymentId the payment ID
	 * @throws ErrorBean if the input data are not correct
	 */
	public void delete (String paymentId) throws ErrorBean {
		pb.remove(new PaymentBean(checkPaymentID(paymentId)));
	}
	
	/* END Methods */
	
	
	/* Data Checks */
	
	private double checkAmount (String amount) throws ErrorBean {
		return checkDouble(amount, PAYMENT_AMOUNT_INVALID, this);}
	
	private Calendar checkDatePaid (String datePaid) throws ErrorBean {
		return checkCalendar(datePaid, ErrorCode.PAYMENT_DATE_NULL, this);}
	
	private int checkOrderID (String orderID) throws ErrorBean {
		return checkInt(orderID, ORDER_ID_INVALID, this);}
	
	private int checkPaymentID (String paymentID) throws ErrorBean {
		return checkInt(paymentID, PAYMENT_ID_INVALID, this);}
	
	/* END Data Checks */
	
}
